package everyday;

import java.lang.reflect.Array;
import java.util.Arrays;

public class T1031 {
    public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
        int n = nums.length;
        int[] sums = new int[n + 1];
        for (int i = 0; i < n; i++) {
            sums[i + 1] = sums[i] + nums[i];
        }
        int secondIndex = firstLen + secondLen;
        int firstIndex = firstLen;
        int maxTwoSum = 0;
        int maxFirstSum = 0;
        while (secondIndex < n + 1) {
            int sumS = sums[secondIndex] - sums[secondIndex - secondLen];
            int sumF = sums[firstIndex] - sums[firstIndex - firstLen];
            maxFirstSum = Math.max(maxFirstSum, sumF);
            maxTwoSum = Math.max(maxTwoSum, maxFirstSum + sumS);
            secondIndex++;
            firstIndex++;
        }
        if (firstLen == secondLen) return maxTwoSum;
        secondIndex = firstLen + secondLen;
        firstIndex = secondLen;
        maxFirstSum = 0;
        while (secondIndex < n + 1) {
            int sumS = sums[secondIndex] - sums[secondIndex - firstLen];
            int sumF = sums[firstIndex] - sums[firstIndex - secondLen];
            maxFirstSum = Math.max(maxFirstSum, sumF);
            maxTwoSum = Math.max(maxTwoSum, maxFirstSum + sumS);
            secondIndex++;
            firstIndex++;
        }
        return maxTwoSum;
    }
}
